package com.example.myapplication;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;

import java.util.List;

class NewsAdapter extends RecyclerView.Adapter<NewsViewHolder> {
    private List<Data> newsList;

    public NewsAdapter(List<Data> newsList) {
        this.newsList = newsList;
    }

    @NonNull
    @Override
    public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_news,viewGroup,false);
        NewsViewHolder newsViewHolder=new NewsViewHolder(view);
        return newsViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull NewsViewHolder newsViewHolder, int i) {

        Data data=newsList.get(i);
        String imgUrl =data.getThumbnail_pic_s();
        if(imgUrl!=null && imgUrl.length()>0){
            Glide.with(newsViewHolder.iv_news.getContext()).load(imgUrl).into(newsViewHolder.iv_news);
        }
        newsViewHolder.tv_title.setText(data.getTitle());
        newsViewHolder.tv_author.setText(data.getAuthor_name());
        newsViewHolder.tv_date.setText(data.getDate());
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }
    public void changeData(List<Data> newsList){
        this.newsList=newsList;
        notifyDataSetChanged();
    }
}
